package com.codebattery.controller;

import com.codebattery.domain.model.Cart;
import com.codebattery.domain.param.CartItemInfo;
import com.codebattery.service.CartService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/cart")
@Slf4j
public class CartController {
    @Resource
    private CartService cartService;

    @ApiOperation(value="添加项目",notes="")
    @RequestMapping(value="/add-item",method = RequestMethod.POST)
    @ResponseBody
    public Cart addItem(@RequestBody CartItemInfo itemInfo) {
        Cart cart = cartService.addItem(itemInfo);
        return cart;
    }

    @ApiOperation(value="查询购物车",notes="")
    @RequestMapping(value="/get-cart",method = RequestMethod.POST)
    @ResponseBody
    public Cart getCart(@RequestBody Map<String, String> param) {
        String userId = Optional.ofNullable(param.get("userId")).orElse(null);
        Cart cart = cartService.getCart(new Integer(userId));
        return cart;
    }

    @ApiOperation(value="删除项目",notes="")
    @RequestMapping(value="/delete-item",method = RequestMethod.POST)
    @ResponseBody
    public void deleteItem(String userId, Integer id, String name) {
        cartService.deleteItem(userId, id);
    }

    @ApiOperation(value="清空购物车",notes="")
    @RequestMapping(value="/empty-cart",method = RequestMethod.POST)
    public void emptyCart(@RequestParam String userId) {
        cartService.empty(userId);
    }
}